module Bars (getDistribution, BarArray) where

import Types
import TableFunction
import Data.Time
import Data.Map
import qualified Data.Map as Map

data BarPeriod = DayBar | HourBar | MinBar
        
type BarArray = Map UTCTime BarData


-- normalizeDistribution :: TFunction -> TFunction
normalizeDistribution f = Map.map (\x -> x/s) f
        where s = Map.fold (\x a -> a+x) 0.0 f

getDistribution :: BarArray->TFunction
getDistribution xs = normalizeDistribution (getPreDistribution (mergeDiffs (getDiffs xs)))

getPreDistribution xs = foldl f empty xs
        where f m x = insertWith' (+) x 1.0 m

getDiffs x = elems (Map.map remapToDiffs x)
        where remapToDiffs a = ((barHigh a)-(barOpen a), (barLow a)-(barOpen a))

mergeDiffs ((a,b):xs) = a : b : mergeDiffs xs
mergeDiffs []         = []